package com.douyin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.douyin.entity.video.VideoStar;
import com.douyin.mapper.VideoStarMapper;
import com.douyin.service.VideoStarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

@Service
public class VideoStarServiceImpl extends ServiceImpl<VideoStarMapper, VideoStar> implements VideoStarService {

    @Autowired
    private  VideoStarMapper videoStarMapper;
    @Override
    public boolean starVideo(VideoStar videoStar) {
        try {
            this.save(videoStar);
        }catch (Exception e){
            VideoStar videoStar1= videoStarMapper.queryOne(videoStar.getVideoId(),videoStar.getUserId());
            if (videoStar1!=null&&videoStar1.getIsDeleted()){
                videoStar1.setIsDeleted(false);
                videoStarMapper.updateVideoStar(videoStar1);
                return true;
            }
            this.remove(new LambdaQueryWrapper<VideoStar>()
                    .eq(VideoStar::getVideoId,videoStar.getVideoId()).eq(VideoStar::getUserId,videoStar.getUserId()));
            return false;
        }
        return true;
    }
}
